/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.yonsei.iwb.sdt.guix;

import com.hp.hpl.jena.rdf.model.Resource;
import java.util.Hashtable;
import java.util.Vector;
import javax.swing.DefaultListModel;
import org.yonsei.iwb.sdt.datastructure.rule.DecisionRule;
import org.yonsei.iwb.sdt.datastructure.rule.DecisionRuleSet;

/**
 *
 * @author DK
 */
public class GDSearchingPanel extends javax.swing.JPanel {

    /**
     * Creates new form GDSearchingPanel
     */
    public GDSearchingPanel(Controller ctrl) {
        this.ctrl = ctrl;
        this.instanceURIListModel = new DefaultListModel();
        drinsTable = new Hashtable();
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        decisionRuleList1 = new org.yonsei.iwb.sdt.guix.DecisionRuleList(this);
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane4 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        setPreferredSize(new java.awt.Dimension(971, 927));

        jButton1.setText("Get Rule List");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextField1.setEditable(false);
        jTextField1.setBackground(new java.awt.Color(255, 255, 255));
        jTextField1.setForeground(new java.awt.Color(204, 204, 204));
        jTextField1.setText("Selected Decision rule");

        decisionRuleList1.setBorder(javax.swing.BorderFactory.createTitledBorder("Decision Rules"));
        jScrollPane2.setViewportView(decisionRuleList1);

        jButton2.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
        jButton2.setLabel("Search");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTextArea1.setEditable(false);
        jTextArea1.setBackground(new java.awt.Color(240, 237, 237));
        jTextArea1.setColumns(20);
        jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jList1.setBorder(javax.swing.BorderFactory.createTitledBorder("Matched Instances"));
        jList1.setModel(this.instanceURIListModel);
        jScrollPane4.setViewportView(jList1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(21, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 639, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jScrollPane1)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 929, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 929, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(21, 21, 21))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane4))
                .addGap(81, 81, 81))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // get rule list
        this.ctrl.defineSDTSearcher();
        this.decisionRuleList1.clear();
        try {
            DecisionRuleSet drs = this.ctrl.getDecisionRuleSet();
            for (int i = 0; i < drs.size(); i++) {
                DecisionRule dr = drs.get(i);
                this.decisionRuleList1.insertDecisionRuleToList(dr.toString(), i);
            }
            CurrentMainFrameStatus.DECISION_RULE_LOADED = true;
        } catch (NullPointerException npe) {
            this.decisionRuleList1.insertDecisionRuleToList("Decision Rule is not exist", 0);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if(CurrentMainFrameStatus.DECISION_RULE_LOADED){
            DecisionRule dr = this.ctrl.getDecisionRuleSet().elementAt(ruleIndex);
            Vector<Resource> rset;
            if (this.drinsTable.containsKey(ruleIndex)) {
                rset = this.drinsTable.get(ruleIndex);
            } else {
                rset = this.ctrl.getMatcherResourceSet(dr);
                if (rset.isEmpty()) {
                    this.instanceURIListModel.addElement("No matched Instances");
                }
                this.drinsTable.put(ruleIndex, rset);
            }
            this.instanceURIListModel.clear();
            for (int i = 0; i < rset.size(); i++) {
                Resource r = rset.get(i);            
                this.instanceURIListModel.addElement(r.toString());
            }
        }else{
            this.instanceURIListModel.clear();
            this.instanceURIListModel.addElement("Decision Rule is not loaded");
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    public void inputSelectRuleToTextField(String s, int index) {
        this.jTextField1.setForeground(new java.awt.Color(0, 0, 0));
//        this.jTextField1.setText(s);
        this.jTextField1.setText(s);
        this.ruleIndex = index;
    }
    
    private Hashtable<Integer, Vector<Resource>> drinsTable;
    private Controller ctrl;
    private int ruleIndex;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.yonsei.iwb.sdt.guix.DecisionRuleList decisionRuleList1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JList jList1;
    private DefaultListModel instanceURIListModel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
